package com.pan.admin.module.api.service.impl;


import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.module.api.dao.ApiKeyBillDao;
import com.pan.admin.module.api.dao.ApiKeyDao;
import com.pan.admin.module.api.entity.po.ApiKeyBillPO;
import com.pan.admin.module.api.entity.qo.ApiKeyBillQO;
import com.pan.admin.module.api.service.ApiKeyBillService;
import com.pan.admin.module.loanProduct.dao.LoanProductDao;
import com.pan.admin.module.loanProduct.dao.LoanProductPlanDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 渠道充值记录
 *
 * @author caoxin
 */
@Service
public class ApiKeyBillServiceImpl implements ApiKeyBillService {

    @Autowired
    private ApiKeyBillDao apiKeyBillDao;
    @Autowired
    private ApiKeyDao apiKeyDao;
    @Autowired
    private LoanProductDao loanProductDao;
    @Autowired
    private LoanProductPlanDao loanProductPlanDao;

    /**
     * 查询渠道充值记录列表
     *
     * @param apiKeyBillQO 渠道充值记录
     * @return 渠道充值记录
     */
    @Override
    @Transactional(readOnly = true)
    public List<ApiKeyBillPO> list(ApiKeyBillQO apiKeyBillQO) {
        List<ApiKeyBillPO> list = this.apiKeyBillDao.list(apiKeyBillQO);
        list.forEach(item -> {
            String apiClientId = item.getApiClientId();
            if(apiClientId == null) {
                return;
            }
            String apiName = this.apiKeyDao.findApiNameByApiCleanId(apiClientId);
            item.setApiName(apiName);
        });
        list.forEach(item -> {
            Long loanProductId = item.getLoanProductId();
            if(loanProductId == null) {
                return;
            }
            String LoanProductName = this.loanProductDao.findLoanProductNameByLoanProductId(loanProductId);
            item.setLoanProductName(LoanProductName);
        });
        list.forEach(item -> {
            Long loanProductPlanId = item.getLoanProductPlanId();
            if(loanProductPlanId == null) {
                return;
            }
            String loanProductPlanName = this.loanProductPlanDao.findLoanProductPlanNameByLoanProductPlanId(loanProductPlanId);
            item.setLoanProductPlanName(loanProductPlanName);
        });
        return list;
    }

    /**
     * 新增渠道充值记录
     *
     * @param apiKeyBillPO 渠道充值记录
     * @return 结果
     */
    @Override
    public int insertApiKeyBill(ApiKeyBillPO apiKeyBillPO) {
        apiKeyBillPO.setCreateTime(DateUtils.getNowDate());
        return this.apiKeyBillDao.insertApiKeyBill(apiKeyBillPO);
    }

}
